package cn.dglydrpy.study.j2ee.manage.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.dglydrpy.study.j2ee.jdbc.utils.DBUtil;
import cn.dglydrpy.study.j2ee.manage.entity.Emp;

public class EmpDAO {
	
	public List<Emp> findAll(){
		String sql = "select empno, ename, mgr, "
				+ "hiredate, deptno, salary, comm "
				+ "from t_emp";
		Connection conn = null;
		try {
			conn = DBUtil.getConn();
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			List<Emp> list = new ArrayList<>();
			while(rs.next()) {
				int empno = rs.getInt("empno");
				String ename = rs.getString("ename");
				int mgr = rs.getInt("mgr");
				Date hiredate=rs.getDate("hiredate");
				int deptno = rs.getInt("deptno");
				double salary = rs.getDouble("salary");
				double comm = rs.getDouble("comm");
				Emp emp = new Emp(empno, ename, mgr, hiredate, deptno, salary, comm);
				list.add(emp);
 			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e); 
		} finally {
			DBUtil.close(conn,null,null); 
		}
	}
}